<?php
declare (strict_types=1);

namespace app\service;

use app\model\FetchUrlModel;
use app\model\FetchTaskModel;
use think\App;

class ManagerService extends \think\Service
{
    public function __construct(App $app, FetchUrlModel $fetchUrlModel, FetchTaskModel $fetchTaskModel)
    {
        // 将参数保存到类的属性中，方便后续使用
        $this->app = $app;
        $this->fetchUrlModel = $fetchUrlModel;
        $this->$fetchTaskModel = $fetchTaskModel;

        parent::__construct($app);
    }

    function createTableBylike()
    {
        foreach ($this->fetchUrlModel->cursor() as $item){
            $table_name = $item->table_name;
            $rs=explode('_', $table_name);

            $like_table=''; // 用like方式创建
            $cteate_table=''; // 将创建的表
            if(count($rs)>1){
                $like_table=$rs[0];
                if($this->tableExists($like_table) and !$this->tableExists($cteate_table)){
                    $sql = "create table if not exists $cteate_table like `$like_table`";
                    Db::query($sql);;
                }
            }
        }
    }

    function tableExists($table_name)
    {
        $sql = "SHOW TABLES LIKE '$table_name'";
        $rs = Db::query($sql);
        return count($rs) > 0;
    }
}
